<?php
class login extends My_Controller
{
	function login()
	{
		parent::__construct();
		$this->load->model('mod_login','login');
		$this->load->library('Hash');
	}
	
	function index()
	{
		if (empty($_SESSION[base_url().'user_email']) && !isset($_SESSION[base_url().'user_email']))
		{
			$data['title']	= 'Đăng nhập';
			$this->load->view('sites/login', $data);
		}
		else
			redirect(base_url());		
	}
	
	function action_login()
	{
		if ($this->input->post('btnlogin'))
		{
			$email			= mysql_real_escape_string($this->input->post('txtemail'));
			$password		= mysql_real_escape_string($this->input->post('txtpassword'));
			$password_hash	= $this->hash->create('cdraovat'.$password, '', 'md5sha512');
			if (!empty($email) && !empty($password))
			{
				$this->login->login_user($email, $password_hash);				
			}
			else
			{
				$_SESSION["error_user_login"] = "Bạn chưa nhập Username | Password";
				redirect(base_url().'dang-nhap');
			}
		}
	}
	
	function action_register()
	{
		if ($this->input->post('btnregister'))
		{
			$email			= mysql_real_escape_string($this->input->post('email'));
			$password		= mysql_real_escape_string($this->input->post('password'));
			$password_hash	= $this->hash->create('cdraovat'.$password, '', 'md5sha512');
			
			$params			= array(
									'email'		=> $email,
									'password'	=> $password_hash,
									'status'	=> 0,
									'contact'	=> mysql_real_escape_string($this->input->post('contact')),
									'address'	=> mysql_real_escape_string($this->input->post('address')),
									'mobile'	=> mysql_real_escape_string($this->input->post('mobile'))
			);		
			if (!empty($email) && !empty($password))
			{
				$this->mod_lib->insert('users', $params);
				
				$to      = 'nobody@example.com';
				$subject = 'the subject';
				$message = 'hello';
				$headers = 'From: webmaster@example.com' . "\r\n" .
					'Reply-To: webmaster@example.com' . "\r\n" .
					'X-Mailer: PHP/' . phpversion();

				mail($to, $subject, $message, $headers);
				redirect(base_url().'dang-ky-thanh-cong');				
			}
		}
	}
	
	function checkemail()
	{
		$email_exist	= array();
		$email_users	= $this->mod_lib->load_all('users', '', array(), '', '', array());
		foreach ($email_users as $val)
		{
			$email_exist[]	= $val['email'];
		}
		
		$email	= mysql_real_escape_string($_POST['email']);
		
		if (!$email)
		{
			echo '<font color=red><br>Email có chứa ký tự đặc biệt</font>';
			return;	
		}
		else if (strlen($email) < 6)
		{
			echo '<font color=red>Email phải có ít nhất 6 ký tự</font>';
			return;
		}
		else if (!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email))
		{
			echo '<font color=red>Email không đúng định dạng</font>';
			return;
		}
		else if (in_array($email, $email_exist))
		{
			echo '<font color=red>Email này đã tồn tại. <a href="'.base_url().'quen-mat-khau.html">Bạn quên mật khẩu</a></font>';
			return;
		}
		else
		{
			echo '';
		}
	}
}